
$(function() {
	
	$("input[id^='template_s']:visible").autocomplete('kq.php?do=Template.ajax_code', {
		width: 200,
		max: 20,
		dataType: "json",
		parse: function(data) {
			return $.map(data, function(row) {
				return {
					data: row,
					value: row.name,
					result: row.id + ":" + row.name
				}
			});
		},
		formatItem: function(item) {
			return item.code + "/" + item.name.trim();
		}
	});
});

function validate_add() {
	var validator = $("#form_add").validate({
		rules: {
			name: "required",
			fullname: "required",
			country: {
				required: true,
				category: true
			},
			address: "required"
		},
		errorPlacement: function(label, element) {
			label.insertAfter(element);
		},
		success: function(label) {
			label.html("&nbsp;").addClass("checked");
		}
	});

	if (!validator.form()) {
		return false;
	}

	for (var i = 0; i < z.length; i++) {
		// alert($.toJSON($("#contact" + z[i] + "").html()));
		var validator = $("#form" + z[i]).validate({
			rules: {
				linkman: "required",
				tel: {
					tel: true
				},
				fax: {
					fax: true
				},
				mobile: {
					mobile: true
				},
				email: {
					email: true
				}
			},
			errorPlacement: function(label, element) {
				label.insertAfter(element);
			},
			success: function(label) {
				label.html("&nbsp;").addClass("checked");
			}
		});

		if (!validator.form()) {
			return false;
		}
	}

	return true;
}

function dialog_add_(url, title, xy) {
	var result = Common.result();
	$('#dialog_add').dialog('option', 'buttons', {

		"关闭": function() {
			$(this).dialog("close");
		},

		"添加": function() {
			if (!confirm("确认是否提交？")) {
				return false;
			}

			var form = $("#form_add")[0];

			var body = { };
			body.template = form.template.value;
			body.t1 = form.date1.value + " " + form.time1.value;
			body.t2 = form.date2.value + " " + form.time2.value;

			body.content = form.content.value;

			$(this).dialog("close");

			$.post('kq.php?do=TemplatePlan.addPost', body, function(data) {
				$(result).html(data);
			});
		}
	});

	dialog_add(url, title, xy);
}